#include "remotecontrolwidget.h"
#include "ui_remotecontrolwidget.h"

#include <QSqlQuery>
#include <QDebug>

RemoteControlWidget::RemoteControlWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RemoteControlWidget)
{
    ui->setupUi(this);
    initUI();
}

RemoteControlWidget::~RemoteControlWidget()
{
    delete ui;
}
void RemoteControlWidget::initUI()
{
    mainLayout = new  QGridLayout;

    ui->pushButton->setVisible(false);//将一个button无效

    mainLayout->addWidget(ui->label,1,1,1,1);
    mainLayout->addWidget(ui->lineEdit,1,2,1,6);
    mainLayout->addWidget(ui->label_2,1,8,1,1);
    mainLayout->addWidget(ui->comboBox,1,9,1,2);
    mainLayout->addWidget(ui->pushButton,1,12,1,1);

    mainLayout->addWidget(ui->label_3,2,1,1,1);
    mainLayout->addWidget(ui->checkBox,2,2,1,2);
    mainLayout->addWidget(ui->label_4,2,8,1,1);
    mainLayout->addWidget(ui->spinBox,2,9,1,2);

    mainLayout->addWidget(ui->pushButton_2,3,2,1,1);
    mainLayout->addWidget(ui->pushButton_3,3,9,1,1);
    setLayout(mainLayout);

    connect(ui->pushButton, SIGNAL(clicked()), this ,SLOT(check()));
    connect(ui->pushButton_2,SIGNAL(clicked()), this, SLOT(rewrite()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this ,SLOT(sendMessage()));

    //
    QSqlQuery query;
    QString sql = trUtf8("select DISTINCT name from devs");
    if (query.exec(sql)) {
        while (query.next()) {
            ui->comboBox->addItem(query.value(0).toString());
        }
    }

    //ui->spinBox->setAttribute(Qt::WA_InputMethodEnabled,false);


}
void RemoteControlWidget::rewrite()
{
    ui->lineEdit->clear();
    ui->comboBox->clear();
    ui->checkBox->setChecked(false);
    ui->spinBox->setValue(0);
}
void RemoteControlWidget::sendMessage()
{
    //此处需要一些相关的检测行为，以保证数据的有效性
    Message me(Parameters::cmsaddr,QString("+86") + ui->lineEdit->text().trimmed(),
               QString(trUtf8("设备：")) + ui->comboBox->currentText().trimmed() + "|"+
               ui->checkBox->text()+":" + (ui->checkBox->isChecked()?"on|":"off|") +
               QString(trUtf8("设定值：")) + ui->spinBox->text() + "|");
    emit sig_message(me);
    //    emit message(ui->lineEdit->text(),ui->lineEdit_2->text(),
    //                 ui->checkBox->isChecked(),ui->spinBox->value());

    emit sig_cmd(ui->comboBox->currentText().toUShort(), (!ui->checkBox->isChecked())?
                     ui->spinBox->value():ui->spinBox->value()+1);

    qDebug() << ui->comboBox->currentText().toUShort() << ((!ui->checkBox->isChecked())?
                    ui->spinBox->value():ui->spinBox->value()+1);


}
void RemoteControlWidget::check()
{

}
